package com.moli.honjie.s002test.presenter;

import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.exception.BleException;
import com.moli.honjie.s002test.ui.ControlActivity;
import com.moli.honjie.s002test.utils.BleUtils;
import com.moli.honjie.s002test.utils.DataUtils;

/**
 * Created by Administrator on 2018/3/26.
 */

public class ControlActivityPresenter {

    private int mCurrentClick = 0;

    private ControlActivity mControlActivity;
    private BleUtils mBleUtil;

    private String mCurrentText = "当前测试: ";

    public ControlActivityPresenter(ControlActivity controlActivity) {
        mControlActivity = controlActivity;
        mBleUtil = BleUtils.getInstance();

        // 打开通知
        mBleUtil.openNotify(mBleNotifyCallback);
    }

    public void clickLed() {
        sendPauseData(mCurrentClick);
        mCurrentClick = 1;
        sendData(mCurrentClick);
    }

    public void clickMotor() {
        sendPauseData(mCurrentClick);
        mCurrentClick = 2;
        sendData(mCurrentClick);
    }

    public void clickTemp() {
        // 温湿度
        sendPauseData(mCurrentClick);
        mCurrentClick = 3;
        sendData(mCurrentClick);

    }

    public void clickHeart() {
        // 心率
        sendPauseData(mCurrentClick);
        mCurrentClick = 4;
        sendData(mCurrentClick);

    }

    public void clickGyro() {
        sendPauseData(mCurrentClick);
        mCurrentClick = 5;
        sendData(mCurrentClick);
    }

    public void clickShutDown() {
        // 关机
        sendPauseData(mCurrentClick);
        mCurrentClick = 6;
        sendData(mCurrentClick);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                onDestroy();
            }
        }, 1000);
    }

    private android.os.Handler mHandler = new android.os.Handler();

    private void sendData(int id) {
        // 发送数据
        switch (id) {
            case 1:
                mBleUtil.sendData((byte) 0x51, (byte) 0x01);
                break;
            case 2:
                mBleUtil.sendData((byte) 0x52, (byte) 0x01);
                break;
            case 3:
                // 紫外线
                mBleUtil.sendData((byte) 0x41);
                break;
            case 4:
                // 心率
                mBleUtil.sendData((byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x01);
                break;
            case 5:
                // 陀螺仪
                mControlActivity.mTvInfo.setText("陀螺仪 \n 注意 \n 请将设备静止一秒以上");
                mBleUtil.sendData((byte) 0x90);
                break;
            case 6:
                mBleUtil.sendData((byte) 0x46);
                break;
            case 7:
                break;
            default:
                break;
        }

        String s = mCurrentText + " " + id;
        mControlActivity.mTvCurrentText.setText(s);
        mCurrentText = s;
    }

    /**
     * 发送停止数据
     */
    public void sendPauseData(int id) {
        switch (id) {
            case 1:
                mBleUtil.sendData((byte) 0x51, (byte) 0x00);
                break;
            case 2:
                mBleUtil.sendData((byte) 0x52, (byte) 0x00);
                break;
            case 3: //  温湿度 紫外线
                mBleUtil.sendData((byte) 0x41, (byte) 0x02);
                break;
            case 4:  // 心率
                mBleUtil.sendData((byte) 0x40, (byte) 0x02);
                break;
            case 5:
                mBleUtil.sendData((byte) 0x90, (byte) 0x02);
                break;
            case 6:
                // mBleUtil.sendData((byte) 0x54, (byte) 0x00);
                break;
        }
    }

    private BleNotifyCallback mBleNotifyCallback = new BleNotifyCallback() {
        @Override
        public void onNotifySuccess() {

        }

        @Override
        public void onNotifyFailure(BleException exception) {

        }

        @Override
        public void onCharacteristicChanged(byte[] value) {

            switch (value[1]) {
                case (byte) 0x41:
                    // 温湿度紫外线
                    if (value[2] == 01) {
                        onEnMeasureDataRecieved(DataUtils.getIntByByte(value[3]), (DataUtils.getIntByByte(value[4]) * 256 + DataUtils.getIntByByte(value[5])) / 10, DataUtils.getIntByByte(value[6]), DataUtils.getIntByByte(value[7]));
                    }
                    break;
                case (byte) 0x40:
                    // 心率
                    if (value[2] == 01) {
                        int intByByte = DataUtils.getIntByByte(value[5]);
                        String text = "心率 \n 心率值: " + intByByte;
                        mControlActivity.mTvInfo.setText(text);
                    }
                    break;
                case (byte) 0x90:
                    // 陀螺仪
                    onGyroscope(value[2], value[3], value[4]);
                    break;
                case (byte) 0x54:
                    byte b = value[3];
                    String text = "";
                    if (b == 01) {
                        text = "VOC检测正常";
                    } else if (b == 0) {
                        text = "VOC检测不正常";
                    }

                    mControlActivity.mTvInfo.setText(text);
                    break;
            }
        }
    };

    private void onGyroscope(byte data1, byte flag, byte sign) {
        // 陀螺仪
        if (data1 == 01) {
            if (flag == 01 && sign == 0) {
                //晃动设备
                mControlActivity.mTvInfo.setText("陀螺仪\n请在3s内摇动设备");
            } else if (flag == 01 && sign == 1) {
                // 显示设备正常
                mControlActivity.mTvInfo.setText("陀螺仪 \n 设备正常");
            } else if (flag == 00 && sign == 1) {
                // 显示设备正常
                mControlActivity.mTvInfo.setText("陀螺仪 \n 设备不正常");
            }
        }
    }

    private void onEnMeasureDataRecieved(int tempState, int temp, int shidu, int ziwaixian) {
        String text =
                "温湿度,紫外线 \n 温度正负值: "
                        + tempState + "\n 温度: "
                        + temp + "℃\n 温湿度: "
                        + shidu + "\n紫外线: "
                        + ziwaixian;
        mControlActivity.mTvInfo.setText(text);
    }


    public void onDestroy() {
        BleUtils.getInstance().closeNotify();
        BleUtils.getInstance().disconnectDevice();
    }
}
